package net.tooeye.easyplus.easyplus.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import net.tooeye.easyplus.easyplus.R;
import net.tooeye.easyplus.easyplus.common.NetworkUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText editTextHost,editTextUser,editTextPassword;
    private Button buttonLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ActionBar bar = getSupportActionBar();
        if (bar != null){
            bar.hide();
        }
        init();
    }

    private void init() {
        editTextHost = findViewById(R.id.editHost);
        editTextUser = findViewById(R.id.editUser);
        editTextPassword = findViewById(R.id.editPassword);
        buttonLogin = findViewById(R.id.buttonLogin);
        SharedPreferences sp = getSharedPreferences("easy_plus", Context.MODE_PRIVATE);
        editTextHost.setText(sp.getString("host",""));
        editTextUser.setText(sp.getString("user",""));
        editTextPassword.setText(sp.getString("password",""));
        editTextPassword.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
        buttonLogin.setOnClickListener(this);
        if (!TextUtils.isEmpty(editTextHost.getText())){
            buttonLogin.callOnClick();
        }
    }

    @Override
    public void onClick(View view) {
        Login();
    }

    private  void Login(){
        Map<String,String> params = new HashMap<>();
        params.put("username",editTextUser.getText().toString());
        params.put("password",editTextPassword.getText().toString());
        params.put("clientId","android");
        String url = "http://" + editTextHost.getText().toString() + "/user/ApiLogin";
        @SuppressLint("HandlerLeak") Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject((String)msg.obj);
                    if (jsonObject.getBoolean("Status")) {
                        SharedPreferences sp = getSharedPreferences("easy_plus", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("host", editTextHost.getText().toString());
                        editor.putString("user", editTextUser.getText().toString());
                        editor.putString("password", editTextPassword.getText().toString());
                        editor.putString("token", jsonObject.getString("Token"));//保存认证TOKEN
                        editor.apply();
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        finish();
                    }else{
                        Toast.makeText(LoginActivity.this, jsonObject.getString("Message"), Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        };
        NetworkUtil.getInstance().post(LoginActivity.this,url,params,handler,1,"");
    }
}
